﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Windows.Navigation;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Phone.Tasks;

namespace BaGiaDanhCuop
{
    public partial class MainPage : PhoneApplicationPage
    {
        ContentManager contentManager;
        GameTimer timer;
        SpriteBatch spriteBatch;
        UIElementRendererHelper uiRenderer;

        Texture2D soundOn_TT, soundOff_TT;
        Sprite soundOn_Sprite, soundOff_Sprite;

        //woman
        Texture2D Woman_Shadow_TT;
        Texture2D Woman_Move_TT;
        Texture2D Woman_Fighting_Fail_TT;
        Texture2D Woman_Fighting_Strong_TT;
        Texture2D Woman_Fighting_Mid_TT;
        Texture2D Woman_Be_Cuffed_TT;

        //font
        public static SpriteFont ItemFont;
       

        public static bool isSound;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            contentManager = (Application.Current as App).Content;

            // Create a timer for this page
            timer = new GameTimer();
            timer.UpdateInterval = TimeSpan.FromMilliseconds(5);
            timer.Update += OnUpdate;
            timer.Draw += OnDraw;

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);
            spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);
            uiRenderer = new UIElementRendererHelper(this);
            

            timer.Start();
            LoadContent();
            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // Stop the timer
            timer.Stop();
            
            // Set the sharing mode of the graphics device to turn off XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);
            uiRenderer.Dispose();
            base.OnNavigatedFrom(e);
        }

        //checktouch
        public void checkTouch()
        {
            TouchCollection touch = TouchPanel.GetState();
            foreach (TouchLocation tch in touch)
            {
                CheckButton(tch);
            }
        }

        public void CheckButton(TouchLocation touch)
        {
            if (touch.State == TouchLocationState.Pressed)
            {
                double x = touch.Position.X;
                double y = touch.Position.Y;
                if (soundOn_Sprite.contain(x, y) && isSound)
                {
                    //Debug.WriteLine("Click soundoff");
                    isSound = false;
                    //AudioManager.StopSounds();
                }
                else if (soundOff_Sprite.contain(x, y) && isSound == false)
                {
                    //Debug.WriteLine("Click soundon");
                    isSound = true;
                    //AudioManager.PlayMusic("bg2");
                }
            }
        }

        private void OnUpdate(object sender, GameTimerEventArgs e)
        {
            checkTouch();
            double delta = e.ElapsedTime.TotalSeconds;
            

        }

        /// <summary>
        /// Allows the page to draw itself.
        /// </summary>
        private void OnDraw(object sender, GameTimerEventArgs e)
        {
            SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);
            uiRenderer.PreRender();
            uiRenderer.Draw();

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            if (isSound)
                soundOn_Sprite.Draw(spriteBatch, SpriteEffects.None);
            else soundOff_Sprite.Draw(spriteBatch, SpriteEffects.None);
            
            spriteBatch.End();
        }

        void LoadContent()
        {
            soundOn_TT = contentManager.Load<Texture2D>("Asset/Bt_SoundOn");
            soundOff_TT = contentManager.Load<Texture2D>("Asset/Bt_SoundOff");

            soundOn_Sprite = new Sprite(new Vector2(220, 410), soundOn_TT);
            soundOff_Sprite = new Sprite(new Vector2(220, 410), soundOff_TT);

            //woman
            Woman_Shadow_TT = contentManager.Load<Texture2D>("Asset/Shadow");
            Woman_Be_Cuffed_TT = contentManager.Load<Texture2D>("Asset/Woman_Be_Cuffed_TT");
            Woman_Fighting_Fail_TT = contentManager.Load<Texture2D>("Asset/Woman_Fighting_Fail_TT");
            Woman_Fighting_Mid_TT = contentManager.Load<Texture2D>("Asset/Woman_Fighting_Mid_TT");
            Woman_Fighting_Strong_TT = contentManager.Load<Texture2D>("Asset/Woman_Fighting_Strong_TT");
            Woman_Move_TT = contentManager.Load<Texture2D>("Asset/Woman_Move_TT");

            GamePage.Woman = new OldWoman(Woman_Move_TT, Woman_Fighting_Fail_TT, Woman_Fighting_Strong_TT, Woman_Fighting_Mid_TT, Woman_Be_Cuffed_TT, Woman_Shadow_TT, 100, 200, GamePage.STATE_MOVE, GamePage.ITEM_EMPTY);

            //font
            ItemFont = contentManager.Load<SpriteFont>("Font/ItemFont");
        }

        // Simple button Click event handler to take us to the second page
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/ShopPage.xaml", UriKind.Relative));
        }

        private void Bt_Info_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Bt_Share_Click(object sender, RoutedEventArgs e)
        {
            ShareLinkTask shareLinkTask = new ShareLinkTask();
            shareLinkTask.Title = "Share BaGiaDanhCuop, FireSky";
            shareLinkTask.LinkUri = new Uri("http://www.windowsphone.com/en-us/store/app/fire-sky/d2f50740-c47e-4c15-b273-87fd68968c6d", UriKind.Absolute);
            shareLinkTask.Message = "I am playing BaGiaDanhCuop. It is very interesting. Now, PLAY with me.";
            shareLinkTask.Show();
        }
    }
}